/**
 * 当前脚本下的js主要用于高拍仪拍摄、图片和PDF预览，以及所有在拍摄页面进行的一系列操作，
 * 这个脚本会有很多jsp页面调用，属于公共的脚本.
 */
var ResSelect;//必须取得分辨率标签对象
var iDeviceIndex = 0;//设备的编号；   0：文档摄像头;1：人像摄像头
var iColorMode = 0;//设定获取的图像的色彩模式；   0: 彩色，1: 灰度，2: 黑白。
var Content;
var Capture;//必须得获取object对象 
var strFilePath = "D:\\Download\\";  //临时存放未质检的图片保存路径
//var sstrFilePath = rootPath + "/upload"; //保存图片到服务器的路径
var szPostfix = ".jpg"; //默认图片格式
var imgeId=0;
var strFileNames=new Array();
var iWaterMarkType = -1; //水印变量
var pageNumber = 1;  //图片页码

//打开设备
function OpenDevice(value){
	//$(".slide_container").css("display","none");
	$("#camero").css('display','');
	var iType = parseInt(value);
	iDeviceIndex = iType;
    if(Capture.OpenDevice(iDeviceIndex) == 0) {
    	//alert("启动高拍仪成功！")
		//WriteInfomation("启动设备"+iType.toString()+"成功");
    	$("#paishe").css("display",'');
		Capture.SetCameraExposure(iDeviceIndex,10);//设置自动曝光
		FillInRes();
		var iW = Capture.GetResolutionWidth(iDeviceIndex, -1);
		var iH = Capture.GetResolutionHeight(iDeviceIndex, -1);
		var strRes = iW.toString() + '*' + iH.toString();
		for(var iDx = 0; iDx < ResSelect.length; iDx++) {
			if(strRes == ResSelect.options[iDx].text) {
				ResSelect.selectedIndex = iDx;
				break;
			}			
		}
	} else {
		alert("启动高拍仪失败,请联系管理员！")
		//WriteInfomation("启动设备"+iType.toString()+"失败");
	}
    //SetFileType(0)		//保存图片格式
    SetCutPageType(1)	//自动切边
    SetResIndex(4);		//设置固定分辨率
    
}

//设置分辨率
function SetResIndex(value){
	var iIndex = parseInt(value);
	var iW = Capture.GetResolutionWidth(iDeviceIndex, iIndex);
	var iH = Capture.GetResolutionHeight(iDeviceIndex, iIndex);
	if(Capture.SetResolutionByWH(iDeviceIndex, iW, iH) == 0) {
		WriteInfomation("设置分辨率成功,将重新打开设备");
	} else {
		WriteInfomation("设置分辨率失败");
	}
}



//追加图片
function append(){
	if($("#paishe").is(":hidden")){
		alert("请先打开高拍仪");
		return;
	}
	var date = new Date();
    var imgName =  date.getFullYear().toString()
   		+(date.getMonth()+1).toString()
   		+date.getDate().toString()
   		+date.getHours().toString()
   		+date.getMinutes().toString()
   		+date.getSeconds().toString();
    //拼接图片名称
    var strFileName = strFilePath + imgName + szPostfix;
   // debugger;
  //获取base64编码
   // var base64Code = Capture.GetBase64String();
	var node = $("#shootTree").tree("getSelected");
	if(!node){
		$.messager.alert("提示","请选中需要查询的图片！","info");
		return;
	}
	//var fprn = $("#fprn").text();
	//var ftimes = $("#ftimes").text();
	var fzyid = $("#fzyid").text();
	var fbarcode = $("#shootFbarcode").text();
	var pagination = "";
	var category = "";
	var isChildren = true; //判断选中的数据是否有父级菜单
		if(node.text != "病案封面"){
			if(!node.children){
				//直接点击图片执行的
				pagination = node.text; 
				var str = pagination.substr(1,(pagination.length-1));//获取页码
				pageNumber = parseInt(str.substr(0,(str.length-1)));
				var parent = $("#shootTree").tree("getParent",node.target); //获取父类菜单编号
				Capture.GrabToFile(strFileName);
				//alert("文件保存为当前目录下的"+strFileName);
				$.ajax({
					url:ctx + "/MedicalShootManage/appendImg",
					dataType:'json',
					type:'post',
					data:{
						//"fprn" : fprn,
						"fzyid" : fzyid,
						//"ftimes" : ftimes,
						"pageNumber" : pageNumber,
						"category" : parent.attributes,
						"originPath" : node.attributes,
						"fbarcode" : fbarcode,
						"savePath" : strFileName,
						"imgName" : imgName,
						//"base64" : base64Code,
						"isChildren" : isChildren
					},
					success:function(data){
						//debugger;
						if(data == true){
							$("#shootTree").tree("reload");
							var index = $("#currentIndex").text();
							//刷新后及时定位到当前操作的分类
							$("#shootTree>li>ul>li:eq("+index+")>div").addClass("tree-node-selected");
						}else{
							alert("插入图片失败，请联系管理员！");
						}
					},
					error:function(){
						alert("插入图片失败，请联系管理员！");
					}
				});
			}else{
				//点击父菜单的情况下执行
				if(node.children !=null || node.children != ""){
					var arr = node.children; 
					isChildren = false;
					for(var i =0;i<arr.length;i++){
						var text = arr[i].text;
						var str = text.substr(1,(text.length-1));
						pageNumber = str.substr(0,(str.length-1));
					}
					pageNumber++;
					category = node.attributes; //图片分类编号
				}else if(node.children ==null ||node.children ==""){
					 category = node.attributes; //图片分类编号
				}
				Capture.GrabToFile(strFileName);
				//alert("文件保存为当前目录下的"+strFileName);
				$.ajax({
					url:ctx + '/MedicalShootManage/appendImg',
					dataType:'json',
					type:'post',
					data:{
						//"fprn" : fprn,
						//"ftimes" : ftimes,
						"fzyid" : fzyid,
						"fbarcode" : fbarcode,
						//"base64" : base64Code,
						"category" : category,
						"savePath" : strFileName,
						"imgName" : imgName,
						"pageNumber" : pageNumber,
						"isChildren" : isChildren
						
					},
					success:function(data){
						//debugger;
						if(data == true){
							$("#shootTree").tree("reload");
							var index = $("#currentIndex").text();
							//刷新后及时定位到当前操作的分类
							$("#shootTree>li>ul>li:eq("+index+")>div").addClass("tree-node-selected");
						}else{
							alert("插入图片失败，请联系管理员！");
						}
					},
					error:function(){
						alert("插入图片失败，请联系管理员！");
					}
				})
			}
		}else{
			alert("不能顶级目录下创建图片，请重新选择！");
			return;
		}
	}

//重新拍摄
function repeat(){
	if($("#paishe").is(":hidden")){
		alert("请先打开高拍仪");
		return;
	}
	var date = new Date();
    var imgName =  date.getFullYear().toString()
   		+(date.getMonth()+1).toString()
   		+date.getDate().toString()
   		+date.getHours().toString()
   		+date.getMinutes().toString()
   		+date.getSeconds().toString();
  //获取base64编码
   // var base64Code = Capture.GetBase64String();
	var node = $("#shootTree").tree("getSelected");
	if(!node){
		$.messager.alert("提示","请选中需要重新拍摄的图片！","info");
		return;
	}
	//var fprn = $("#fprn").text();
	//var ftimes = $("#ftimes").text();
	var fzyid = $("#fzyid").text();
	var fbarcode = $("#shootFbarcode").text();
	var originPath = node.attributes; //原图片路径
	var src = originPath.replace(/\\/g,'/');
	var imgSrc = src.substring((src.lastIndexOf("/")+1),src.length);
	var imgsrc = imgSrc.substr(0,imgSrc.indexOf("."));
	//拼接图片路径
	var saveFileName = strFilePath + imgsrc + szPostfix;
	var ID = node.id; //获取当前页ID
	var id = ID.substr(0,(ID.indexOf(',')));
	if(node.children){
		alert("请选中需要重拍的图片！");
		return;
	}else{
		var parent = $("#shootTree").tree("getParent",node.target);
		var arr = parent.children; 
		var fatherCategory = parent.attributes; //图片分类 
		for(var i =0;i<arr.length;i++){
			var text = arr[i].text;
			var str = text.substr(1,(text.length-1));
			pageNumber = str.substr(0,(str.length-1));
		}
		
		$.ajax({
			url:ctx + "/MedicalShootManage/repeatShoot",
			dataType:'json',
			type:'post',
			data:{
				//"fprn" : fprn,
				//"ftimes" : ftimes,
				"fzyid" : fzyid,
				//"base64Code" : base64Code,
				"fbarcode" : fbarcode,	
				"originPath" : originPath,
				"fatherCategory" : fatherCategory,
				"pageNumber" : pageNumber,
				"saveFileName" :saveFileName ,
				"imgName" : imgSrc,
				"id" : id
			},
			success:function(data){
				//debugger;
				if(data == true){
					$("#shootTree").tree("reload");
					var index = $("#currentIndex").text();
					//刷新后及时定位到当前操作的分类
					$("#shootTree>li>ul>li:eq("+index+")>div").addClass("tree-node-selected");
					//当服务器有相同的图片时是不能创建相同的图片，修改数据库记录成功后再执行创建文件操作，然后加密。
					Capture.GrabToFile(saveFileName);
					//alert("文件保存为当前目录下的"+saveFileName);
					//console.info("文件保存为当前目录下的"+saveFileName);
					$.ajax({
						url:ctx + "/MedicalShootManage/createPicture",
						dataType:'json',
						type:'post',
						data:{
							'strFileName' : saveFileName
						},
						success:function(data){
							if(data.msg ==true){
								//alert("加密成功！");
								console.info("加密成功！");
							}
						}
					});
					
				}else{
					alert("重拍图片失败，请联系管理员！");
				}
			},
			error:function(){
				alert("重拍图片失败，请联系管理员！");
			}
		})
		
	}
}

//删除照片
function remove(){
	var node = $("#shootTree").tree("getSelected");
	if(!node){
		$.messager.alert("提示","请选中需要删除的图片！","info");
		return;
	}
	//var fprn = $("#fprn").text();
	//var ftimes = $("#ftimes").text();
	var fzyid = $("#fzyid").text();
	if(node.children){
		$.messager.alert("提示","请选中需要删除的图片！","info");
		return;
	}else{
		var parent = $("#shootTree").tree("getParent",node.target);
		var fatherCategory = parent.attributes; //图片分类 
		var text = node.text;
		var str = text.substr(1,(text.length-1));
		pageNumber = str.substr(0,(str.length-1));
		var ID = node.id; //获取当前页ID
		var id = ID.substr(0,(ID.indexOf(',')));
		//debugger;
		$.ajax({
			url:ctx + "/MedicalShootManage/removePicture",
			dataType : 'json',
			type : 'post',
			data : {
			//	"fprn" : fprn,
			//	"ftimes" : ftimes,
				"fzyid" : fzyid,
				"fatherCategory" : fatherCategory,
				"pageNumber" : pageNumber,
				"path" : node.attributes,
				"id" : id 
			},
			success : function(data){
				//debugger;
				if(data ==true){
					$("#shootTree").tree("reload");
					var index = $("#currentIndex").text();
					//刷新后及时定位到当前操作的分类
					$("#shootTree>li>ul>li:eq("+index+")>div").addClass("tree-node-selected");
				}else{
					alert("删除失败，请联系管理员！");
				}
			},
			error : function(){
				alert("删除失败，请联系管理员！");
			}
		});
	}
	
}
	

//拍照并且存档
function CaptureToFile(isAppend,isRepeat,isShoot) {
	 var date = new Date();
    var imgName =  date.getFullYear().toString()
    		+(date.getMonth()+1).toString()
    		+date.getDate().toString()
    		+date.getHours().toString()
    		+date.getMinutes().toString()
    		+date.getSeconds().toString();
	if(iDeviceIndex == 2){ //双摄像头
		var strFileName = new Array();
		strFileName[0] = strFilePath + imgName + "-Combined" + szPostfix;
		var strDocFileName = strFilePath + imgName+ "-SplitedDOC" + szPostfix;
		var strPorFileName = strFilePath + imgName + "-SplitedPOR" + szPostfix;
		strFileName[1] = strDocFileName + ";" + strPorFileName;
		
		for(var iDx = 0; iDx < 2; iDx++){
			if(Capture.GrabToFile(strFileName[iDx]) == 0){
				//alert("文件保存为当前目录下的"+strFileName[iDx]);
				if(iDx == 0){
					if(checkMultiSource.checked){
						Capture.ImagesToMultiPageFile(strFileName[iDx],0);
					}
					if(szPostfix != ".pdf" && szPostfix != ".tif"){
						strFileNames.push(strFileName[iDx]);
						//Preview(0);
						//此测试页面只保存最后2次拍摄的文件名作为合并图像参数文件名	
						strMergeSource1 = strMergeSource2;
						strMergeSource2 = strFileName[iDx];
					}		
					var strBarcodeContent = "";
					if (checkBarcode.checked){    
						var iBarcodeCount = Capture.GetBarcodeCount();
						WriteInfomation("识别到"+iBarcodeCount.toString()+"个条码");
						for(var k = 0; k < iBarcodeCount; k++){
							WriteInfomation("条码"+k.toString()+":["+Capture.GetBarcodeTypeName(k)+"]"+Capture.GetBarcodeContent(k));
							// strBarcodeContent += "[";
							// strBarcodeContent += Capture.GetBarcodeTypeName(k);
							// strBarcodeContent += "]";
							// strBarcodeContent += Capture.GetBarcodeContent(k);
						}
					// getBarcodeInfo(iBarcodeCount,strBarcodeContent);
					}
				}else{
					if(checkMultiSource.checked){
						Capture.ImagesToMultiPageFile(strDocFileName,0);
						Capture.ImagesToMultiPageFile(strPorFileName,0);
					}
					if(szPostfix != ".pdf" && szPostfix != ".tif"){
						strFileNames.push(strDocFileName);
						//Preview(0);  
						strFileNames.push(strPorFileName);
						//Preview(0);
						//此测试页面只保存最后2次拍摄的文件名作为合并图像参数文件名	
						//strMergeSource1 = strDocFileName;
						//strMergeSource2 = strPorFileName;
					}		
					var strBarcodeContent = "";
					if (checkBarcode.checked){    
						var iBarcodeCount = Capture.GetBarcodeCount();
						//把相关信息写入控制台中
						//WriteInfomation("识别到"+iBarcodeCount.toString()+"个条码");
						for(var k = 0; k < iBarcodeCount; k++){
							WriteInfomation("条码"+k.toString()+":["+Capture.GetBarcodeTypeName(k)+"]"+Capture.GetBarcodeContent(k));
							// strBarcodeContent += "[";
							// strBarcodeContent += Capture.GetBarcodeTypeName(k);
							// strBarcodeContent += "]";
							// strBarcodeContent += Capture.GetBarcodeContent(k);
						}
					// getBarcodeInfo(iBarcodeCount,strBarcodeContent);
					}
				}
			}
		}
		//imgeId++;
	}else{
		var strFileName;
		var categoryName = "";
		//拼接图片名称
		strFileName = strFilePath + imgName + szPostfix;
		//获取base64编码
		var node = $("#shootTree").tree("getSelected");
		var attr = "";
		if(node ==null || node==""){
			alert("请选中图片分类!");
			return;
		}else if(node.iconCls){
			alert("不能选择图片作为分类，请重新选择!");
			return;
		}else if(node.id =="01"){
			alert("不能选择顶级父类作为分类，请重新选择!");
			return;
		}else{
			attr = node.attributes;
		}
		var srcPath = Capture.GetBase64String();
		Capture.GrabToFile(strFileName);
		if($("#fprn").text() =="" || $("#ftimes").text() == ""){
			alert("病案号和次数不能为空!");
			return;
		}
		//获取页码
		if(node.children ==null ||node.children ==""){
			var attr = node.attributes; //图片分类编号
			categoryName = node.text;
			pageNumber = 1; 
		}else if(node.children){
			var arr = node.children; 
			categoryName = node.text;
			for(var i =0;i<arr.length;i++){
				var text = arr[i].text;
				var str = text.substr(1,(text.length-1));
				pageNumber = str.substr(0,(str.length-1));
			}
			pageNumber++;
		}
		browseArea(srcPath,pageNumber,imgName+szPostfix,categoryName);   //预览图片
		//将base64编码传入后台
		$.ajax({
			url:ctx + "/MedicalShootManage/saveBase64",
			dataType:'json',
			type:'post',
			data:{
				//"base64" : base64Code,
				//"ftimes" : $("#ftimes").text(),
				//"fprn" : $("#fprn").text(),
				"fzyid" : $("#fzyid").text(),
				"barcode" : $("#shootFbarcode").text(),
				"pageNumber" : pageNumber,
				"address" : strFileName,
				"pictureName" : imgName,
				"code":attr
				
			},
			success:function(data){ 
				if(data ==true){
					pageNumber++;
					$("#shootTree").tree("reload"); 
					var index = $("#currentIndex").text();
					//刷新后及时定位到当前操作的分类
					$("#shootTree>li>ul>li:eq("+index+")>div").addClass("tree-node-selected");
					
				}else{
					alert("保存失败，请联系管理员！");
				}
			},
			error:function(){
				alert("保存失败，请联系管理员!");
			}
		});

	}
    
}

//即拍即预览
function browseArea(srcPath,pageNumber,imgName,categoryName){
	$("#shootSlider").html( 
			 "<div class='wrapper'>"
		         +"<div id='Shoot_Viewer' class='viewer iviewer_cursor'></div><br/>"
		    +"</div>"
		    
		    + "<div class='caption'>" +
			"<span class='PageNumber'>第"+pageNumber+"页&nbsp;"+categoryName+"&nbsp;"+imgName+"</span>" +
		"</div>" 	    
	);  
	 var $PA = jQuery; 
     var iviewer = {};
     $PA("#Shoot_Viewer").iviewer({
	      src:"data:image/jpg;base64," + srcPath,
	      initCallback: function(){ 
	        iviewer = this;
	      } 
     }); 
}

//切边类型
function SetCutPageType(value){
	var iType = parseInt(value);
	if(Capture.SetCutPageType(iDeviceIndex,iType) == 0){
		WriteInfomation("设置切边方式为"+iType);
	}
}

//读取分辨率列表
function FillInRes(){	
	var iResVecLenth=ResSelect.options.length;
	var iResWidth=0;
	var iResHeight=0;
	for(var i=0;i<iResVecLenth;i++){
		ResSelect.options.remove(0);	//清除分辨率select选项内容
	}
	iResVecLenth = Capture.GetResolutionCount(iDeviceIndex);
	for(var i=0;i<iResVecLenth;i++){
		iResWidth = Capture.GetResolutionWidth(iDeviceIndex,i);
		iResHeight = Capture.GetResolutionHeight(iDeviceIndex,i);
		var strRes = iResWidth.toString()+'*'+iResHeight.toString();
		//var strRes = "1920"+'*'+"1080";			//设置固定分辨率值
		var resopp = new Option(strRes,i.toString());
		ResSelect.add(resopp);
	}
}

//把相关信息写入操作记录编辑框中
function WriteInfomation(strInfo){
	Content.value += ("\r\n"+ strInfo);
}

//摄像旋转
function SetDeviceRotation(rotation){
    var nRotation = parseInt(rotation);
	if(nRotation%90 != 0){
		Capture.SetDeviceRotate(iDeviceIndex,0);//不能被90整除的默认不旋转
	}
    //alert(rotation);
    Capture.SetDeviceRotate(iDeviceIndex,nRotation);
}

//连续拍摄
function StartContinuous()
{
	Capture.StartContinuous(strFilePath,0);	
}

//设置水印参数值
function SetWaterMark(value)
{
	iWaterMarkType = parseInt(value);
}

//添加水印
function AddWaterMark()
{
	switch(iWaterMarkType)
	{
		case -1://不添加水印
			Capture.WaterMark("",-1,0,0,0,0);//参数设置为-1则不添加水印
			break;
		case 0://文字水印
			Capture.WaterMark(WaterContent.value,0,parseInt(WaterTran.value),parseInt(sWaterMarkPos.value),
			parseInt(WaterFontSize.value),parseInt(WaterColor.value));//设置空字符串，并且水印类型设置为0，则会添加时间水印：格式为年月日
			break;
		case 1://图片水印
			Capture.WaterMark(WaterContent.value,1,parseInt(WaterTran.value),parseInt(sWaterMarkPos.value),
			parseInt(WaterFontSize.value),parseInt(WaterColor.value));
			break;
		case 2://日期水印
			Capture.WaterMark(WaterContent.value,2,parseInt(WaterTran.value),parseInt(sWaterMarkPos.value),
			parseInt(WaterFontSize.value),parseInt(WaterColor.value));
			break;
		default:
			break;		
	}
}

//预览水印
function ShowWaterMark()
{
	if (checkShowWaterMark.checked) {
        if(Capture.ShowWaterMark(0)==0)
		{
			WriteInfomation("开启水印预览成功");
		}
		else
		{
			WriteInfomation("开启水印预览失败");
		}
    }else{
        if(Capture.ShowWaterMark(1)==0)
		{
			WriteInfomation("取消水印预览成功");
		}
		else
		{
			WriteInfomation("取消水印预览失败");
		}
    }
}

//去除阴影
function SetResuceShadow()
{
	//var i = checkReduceShadow
	if(checkReduceShadow.checked)
	{
		if(Capture.ReduceShadow(0) == 0)
		{
			WriteInfomation("开启去除阴影功能成功");
		}
		else
		{
			WriteInfomation("开启去除阴影功能失败");
		}
	}
	else
	{
		if(Capture.ReduceShadow(1) == 0)
		{
			WriteInfomation("关闭去除阴影功能成功");
		}
		else
		{
			WriteInfomation("关闭去除阴影功能失败");
		}		
	}
}

// 文档变形修补
function SetRepairDistortion()
{
	//var i = checkReduceShadow
	if(checkRepairDistortion.checked)
	{
		if(Capture.RepairDistortion(0) == 0)
		{
			WriteInfomation("开启文档形变修补功能成功");
		}
		else
		{
			WriteInfomation("开启文档形变修补功能失败");
		}
	}
	else
	{
		if(Capture.RepairDistortion(1) == 0)
		{
			WriteInfomation("关闭文档形变修补功能成功");
		}
		else
		{
			WriteInfomation("关闭文档形变修补功能失败");
		}		
	}
}




//直接点击图片执行的方法
function putImg(node){
//	$(".slide_container").css("display","");
//	$("#camero").css('display','none');
	var id = node.id; 	//id绑定了图片编号
	var Src = node.attributes;
	var imgNumber = id.substring(id.lastIndexOf(",")+1,id.length);	//拆分图片编号
	
	$("#shootSlider").siblings("a").remove(); //先清空插件的元素
	if(Src.substring(Src.lastIndexOf(".")+1,Src.length) == "jpg"){
	 
		 $(".bgSliderIMG").css("display",'none');
    	 $(".SliderIMG").css("display",'none');
    	 
		var imgName = "";
		var parent = $("#shootTree").tree("getParent",node.target);
		var pageNumber = node.text;
		var path = node.attributes;
		var parentText = parent.text;//父类标题
		var cateNumber = parent.attributes;//图片分类编码
		var src2 = Src.replace(/\\/g,'/');
		var index = src2.lastIndexOf("/");
		var imgSrc = src2.substring(index+1,Src.length);
		
		var srcPath=ctx + "/upload/"+sysFcode+"/"+imgSrc; 
		$("#shootSlider").html( 
				 "<div class='wrapper'>"
			         +"<div id='ShootViewer' class='viewer iviewer_cursor' style='overflow: scroll;'></div><br/>"
			    +"</div>"
		     
			 	+ "<div class='caption'>" +
					"<span class='PageNumber'>"+pageNumber+"&nbsp;"+parentText+"&nbsp;"+imgSrc+"</span>" +
					"<span class='auditImgNumber' style='display:none;'>"+imgNumber+"</span>" +
					"<span class='imgCategory' style='display:none;'>"+cateNumber+"</span>" +
					"<span class='imgPath' style='display:none;'>" + path + "</span>" +
					"<span class='pageCode' style='display:none;'>" + pageNumber + "</span>" +
				"</div>" );  
		 
			    $("#shootSlider").removeClass("slide");
			    slider_RsViewer("ShootViewer",srcPath);
				
			 	if(!node.children){ 
					$(".slide_nav").css("display",'none')
				}    
	}else{
		return ;
	}
}


//放置图片至浏览区域
function putImg2(node){
	//$(".slide_container").css("display","");
   // $("#camero").css('display','none');
	var imgName = "";
	var html = "";
	var id = node.id; 	//id绑定了图片编号
	var imgNumber = id.substring(id.lastIndexOf(",")+1,id.length);	//拆分图片编号
	var parent = $("#shootTree").tree("getNode",node.target);
	var parentText = parent.text;//父类标题
	$("#currentModel").html(parent.text);
	var cateNumber = parent.attributes;//图片分类编码
	var parentChildren = parent.children;//父类的子节点数组
	var path = "";
	var isMore = true; //为true，则解密多张图片
	for (var j = 0; j < parentChildren.length; j++) {
		var originPath = parentChildren[j].attributes; //图片路径
		if(j >0){
			path += ","+originPath;
		}else{
			path += originPath;
		}
	}
	$.ajax({
		url: ctx + "/MedicalShootManage/encodeIMG",
		dataType : "json",
		type : "post",
		data : {
			"path" :path,
			"isMore" : isMore
		},
		success:function(){ 
			$(".bgSliderIMG").css("display",'none');
	    	$(".SliderIMG").css("display",'none');
	    	 
			$("#shootSlider").siblings("a").remove(); //先清空插件的元素
			//循环获取子节点
			for (var i = 0; i < parentChildren.length; i++) {
				var src = parentChildren[i].attributes; //图片路径
				var pageNumber = parentChildren[i].text;//图片页码
				var id = parentChildren[i].id; //图片编号
				//拆分图片编号
				var imgNumber = id.substring(id.lastIndexOf(",")+1,id.length);
				//截取图片路径
				var src2 = src.replace(/\\/g,'/');
				var index = src2.lastIndexOf("/");
				var imgSrc = src2.substring(index+1,src.length);
				var srcPath=ctx + "/upload/"+fcode+"/"+imgSrc;
				html += "<li>"  
					 //+"<div class='wrapper'>"
			        // +"<div id='viewer"+i+"'"+" class='viewer iviewer_cursor' style='height:98%;overflow:scroll;border:0px solid #000000;'></div><br/>"
			         //+"</div>" 
					+ "<img src='"+ srcPath +"' onclick=imgClick('"+srcPath+"')>" 
					
						+"<div class='caption'>" +
							"<span class='PageNumber'>" + pageNumber + "&nbsp;" + parentText + "&nbsp;" + imgSrc + "</span>" +
							"<span class='auditImgNumber' style='display:none;'>" + imgNumber + "</span>" +
							"<span class='imgCategory' style='display:none;'>" + cateNumber + "</span>" +
							"<span class='imgPath' style='display:none;'>" + src + "</span>" +
							"<span class='pageCode' style='display:none;'>" + pageNumber + "</span>" +
						"</div>" +
					"</li>";    
			}
			
			$("#shootSlider").html(html); 
			slider(); //渲染特效
			
			/* Ie8浏览器 对于轮番插件和鼠标滚动图片放大插件不兼容
			for (var i = 0; i < parentChildren.length; i++) { 
				var src = parentChildren[i].attributes; //图片路径 
				//截取图片路径
				var src2 = src.replace(/\\/g,'/');
				var index = src2.lastIndexOf("/");
				var imgSrc = src2.substring(index+1,src.length);
				
				var srcPath=ctx + "/upload/"+sysFcode+"/"+imgSrc;
				slider_RsViewer("viewer"+i,srcPath); 
			} */
		}, 
		error:function(){
		//	$.messager.alert("提示","解密图片失败,请联系管理员！","info");
		}
	});
	
} 

//图片播放效果
function slider(){	
  $("#shootSlider").responsiveSlides({
	    auto: false,
	    pager: false,
	    nav: true,
	    speed: 500,
	    // 对应外层div的class : slide_container
	    namespace: "slide"
   });
}

//关闭遮盖层
function closed(){   
    $(".bgSliderIMG").css("display",'none');
    $(".SliderIMG").css("display",'none');
}


//打开遮盖层——图片滚动放大和旋转
function imgClick(srcPath){ 
  //var srcPath = $("li").find('img').prop("src"); 
  $(".bgSliderIMG").css("display",'block');
  $(".SliderIMG").css("display",'block');
  $("#shoot_SliderIMG").html("");   //先清空
  $("#shoot_SliderIMG").html(
		    "<div class='wrapper'>"
          	+ "<div id='shoot_SliderIMG_iviewer' class='viewer iviewer_cursor'></div><br/>"
            + "</div>" ); 
  
  slider_RsViewer("shoot_SliderIMG_iviewer",srcPath);
}

 
//鼠标滚动_图片放大效果
function slider_RsViewer(iviewerId,srcPath){   
	 /* 带有图片旋转功能的时候才用此*/
	  var $PA = jQuery; 
	  var viewer;
      viewer = $PA("#"+iviewerId).iviewer({
    	  src:srcPath
      });
     
	/* var $PA = jQuery; 
     var iviewer = {};
     $PA("#"+iviewerId).iviewer({
	      src:srcPath,
	      zoom: "fit",
	      zoom_min:30,
	      zoom_max: 400,
	      initCallback: function()
	      {   
	        iviewer = this;
	      } 
     });   */
}   

//关闭浏览页
function Close_Shoot_Dialog(){
	$("#"+dialog+"").dialog("close");
}

//浏览照片
function previewImg(){
	var node = $("#shootTree").tree("getSelected");
	 if(node.text != "病案封面"){
	    	//点击分为两种情况：情景一，直接点击图片;情景二：点击二级菜单;
	    		if(!node.children){//情景一
	    			var src = node.attributes;
	    			var isMore = false; //为false，则表示单张图片
	    			var Parent = $("#shootTree").tree("getParent",node.target);
	    			$("#currentModel").html(Parent.text);
	    			$.ajax({
	    				url: ctx + "/MedicalShootManage/encodeIMG",
	    				dataType : 'json',
	    				type : 'post',
	    				data:{
	    					"path" : src,
	    					"isMore" : isMore
	    				},
	    				success:function(){
	    					putImg(node);
	    				},
	    				error:function(){
	    					$.messager.alert("提示","解密图片失败,请联系管理员！","info");
	    				}
	    			});
	    		}else{ //情景二
	    			//如果当前二级菜单没有任何图片，则不执行以下方法
	    			if(node.children.length > 0){ 
	    				putImg2(node);
	    			}else{
	    				return;
	    			}
	    		}
		}
}


//根据病案号查询对应的图片
function queryByFprn(){
	var fprn = $("#fprn").text();
	var value = $("#shootFprnInput").textbox("getValue");
	if(value != ""){
		$("#shootTree").tree({    
		    url: ctx + "/MedicalShootManage/tree",  
		    animate: true,
		    checkbox:false,
		    queryParams:{
		    	"fprn":fprn
		    }
		});
	}
	
} 

//键盘事件  
$(document).keydown(function (event) {
	e = event ? event : (window.event ? window.event : null);
	switch(e.keyCode){
		case 49: // (字母上一排 1)  打开高拍仪
			OpenDevice(CammeraType.value);
			break;
		case 113: //F2  拍摄
			CaptureToFile();
			break;
		case 53: //(字母上一排5 )传送
			transfer();
			break;
		case 51: //(字母上一排 3 ) 旋转
			SetDeviceRotation(90);
			break;
		case 108: //Enter 根据病案号搜索
			queryByFprn();
			break;
		case 52:  //(字母上一排 4 ) 插入
			append();
			break;
		case 50: //(字母上一排 2 )重拍
			repeat();
			break;
		case 54: //(字母上一排 6 )删除
			remove();
			break;
		case 27: //退出
			Close_Shoot_Dialog();
			break;
		case 40: //键盘　Down Arrow 选择树节点事件
			Arrow(40);
			break;
		case 38://键盘　Up Arrow 选择树节点事件
			Arrow(38);
			break;
	}
});  

//键盘Up Arrow 和　Down Arrow 选择树节点事件
function Arrow(value){
	var node = $("#shootTree").tree("getSelected");
	//获取选中的节点
	var items = $("#shootTree .tree-node-selected");
	var index = items.parent().index();
	 if(index < 0){
		 $.messager.alert('提示','请选中分类','info');
		 return;
	 }
	 if(value == 40){
		 if(node.id == "01"){
			$("#shootTree li:eq(0)>div:eq(0)").removeClass("tree-node-selected");   
			 $("#shootTree>li>ul>li:eq(0)>div").addClass("tree-node-selected");
		 }else{
			 $("#shootTree>li>ul>li:eq("+(index+1)+")>div").addClass("tree-node-selected");
			 $("#currentIndex").html(index+1); //将当前的选中的元素下标保存，以供树节点刷新后定位到当前操作的菜单
			 items.removeClass("tree-node-selected");
		 }
	 }else{
		 if(index != 0){
			 $("#shootTree>li>ul>li:eq("+(index-1)+")>div").addClass("tree-node-selected");
			 $("#currentIndex").html(index-1);
			 items.removeClass("tree-node-selected");
		 }
	 }
}


//传送
function transfer(){
	$.messager.confirm("提示","确定要传送记录吗？",function(r){
		if(r){
			$.ajax({
				url :ctx + '/MedicalShootManage/transfer',
				dataType : 'json',
				type : "post",
				data : {
					'fzyid' : $("#fzyid").text(),
					'fprn' : $("#fprn").text(),
					'ftimes' : $("#ftimes").text(),
					'fname' : $("#fname").text(),
					'fcydate' : $("#shootFcydate").text()
				},
				success:function(data){
					if(data.msg == true){
						$.messager.alert('提示','传送成功！','info');
					}else if(data.msg == 'error'){
						$.messager.alert('提示','传送失败，请联系管理员！','info');
					}else{
						$.messager.alert('提示','数据已传送！','info');
					}
				},
				error:function(){
					$.messager.alert('提示','传送失败，请联系管理员！','info');
				}
			});
		}
	});
}

//归档操作
function archivingOperation(){
	$.messager.confirm("提示","确定进行归档操作？",function(r){
		if(r){
			$.ajax({
				url :ctx + '/Archiving/archivingOperation',
				dataType : 'json',
				type : "post",
				data : {
					'fzyid' : $("#fzyid").text(),
					'fprn' : $("#fprn").text(),
					'ftimes' : $("#ftimes").text(),
					'fname' : $("#fname").text()
				},
				success:function(data){
					if(data == "200"){
						$.messager.alert('提示','归档成功！','info');
					}else{
						$.messager.alert('提示','归档失败，请联系管理员！','info');
					}
				},
				error:function(){
					$.messager.alert('提示','归档失败，请联系管理员！','info');
				}
			});
		}
	});
}
